<?php
/**
 * Created by PhpStorm.
 * User: Tangyijun
 * Date: 2017/3/20
 * Time: 14:44
 */
namespace app\home\controller;
use app\models\Bases;
use think\Controller;
class Login extends Base{
    /**
     * @return array
     * 用户登录
     */
    public function login(){
        try{
            $post = input('param.');
            $userInfo = self::getUserInfo("username = '{$post['username']}'");//验证帐号是否存在
            if($userInfo['status']==2){
                throw new \LogicException('用户被禁用',10031);
            }
            self::checkPassword($post['password'],$userInfo['password']);     //验证用户输入的密码是否正确
            self::checkCaptcha($post['captcha']);                             //验证验证码是否正确
            $user = new Bases('user');                                        //更新用户最近登录时间
            if(!$user->setField("username = '{$post['username']}'",['last_login_time'=>time()])){
                throw new \LogicException('登录失败',10031);
            }

            cookie('uid',$userInfo['uid']);                                    //验证通过，将用户的ID编号保存cookie中,后面可以根据uid获取用户信息
        }catch(\Exception $e){
            return json(self::formatResult($e->getCode(),$e->getMessage()));
        }
        return json(self::formatSuccessResult(['type'=>$userInfo['type']]));
    }
}